/**
 * 
 */
package profile.report;

import java.util.ArrayList;
import java.util.List;

import profile.result.ParamResultItem;

/**
 * @author ductrung.tran
 *
 */
public class ParamResultReport {

	private String params;
	
	private List<ParamResultItem> items = new ArrayList<ParamResultItem>();
	
	private long totalExecTime;
	private long totalCorbaTime;
	private long totalServerTime;

	private void addItem(ParamResultItem item) {
		totalExecTime += item.getExecTime();
		totalCorbaTime += item.getCorbaTime();
		totalServerTime += item.getServerTime();
		
		items.add(item);
	}
	
	public int getCount() {
		return items.size();
	}
	
	public long getTotalExecTime() {
		return totalExecTime;
	}
	
	public long getTotalCorbaTime() {
		return totalCorbaTime;
	}
	
	public long getTotalServerTime() {
		return totalServerTime;
	}

	public List<ParamResultItem> getItems() {
		return items;
	}
	
	public void setParam(String params) {
		this.params = params;
	}

	public String getParam() {
		return params;
	}

	public void addParamItems(List<ParamResultItem> resultItemList) {
		if (resultItemList.isEmpty()) {
			throw new RuntimeException("result items is empty");
		}
		for (ParamResultItem item : resultItemList) {
			addItem(item);
		}
	}

	public String toString(int level) {
		return shift(level) + params + "\n" + itemstoString(level+1);
	}

	private String itemstoString(int level) {
		StringBuilder builder = new StringBuilder();
		for (ParamResultItem item : items) {
			builder.append(shift(level)).append("execTime:").append(item.getExecTime())
				.append(",corbaTime:").append(item.getCorbaTime()).append(",serverTime:")
				.append(item.getServerTime());
		}
		return builder.toString();
	}

	private String shift(int level) {
		StringBuilder builder = new StringBuilder();
		for (int i = 1; i <= level; i++) {
			builder.append("  ");
		}
		
		return builder.toString();
	}
	
}
